package com.ggkt.security;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 常用的功能方法 工具类,敏感信息脱敏方法；
 * </p>
 * 
 * @author ggkt
 */
public class MyUtils {

	private static Log log = LogFactory.getLog(MyUtils.class);
	
	/**
	 * 是否为手机号
	 * 
	 * @param number
	 */
	public static boolean isPhone(String number) {
	    return number != null && number.matches("^1[3456789]\\d{9}$");
	}
	
	/**
	 * 对手机号进行模糊处理
	 * @param phone
	 */
	public static String obfuscatePhone(String phone) {
	    return phone.substring(0, 3) + "****" + phone.substring(7);
	}

	/**
	 * @Description:  对联系方式进行脱敏，手机号隐藏中间4位，微信、QQ隐藏中间几位
	 * @Author: xuhuibin
	 * @Date: 2021/5/25 21:54
	 */
	public static String obfuscateContact(String info) {
		if(info!=null){
			String s = null;
			String re = "";
			String v = "";
			String desStr = info;

			if(info.indexOf("@") > 0){
				String [] str = info.split("@");
				desStr = str[0];
			}

			if(desStr.length() > 4){
				if(info.indexOf("@") > 0){
					for(int i=0;i<desStr.length()-3;i++){
						re = re+"*";
					}
					v = desStr.substring(3, desStr.length());
				}else{
					if(desStr.length() > 10){
						for(int i=0;i<desStr.length()-7;i++){
							re = re+"*";
						}
						v = desStr.substring(3, desStr.length()-4);
					}else{
						for(int i=0;i<desStr.length()-4;i++){
							re = re+"*";
						}
						v = desStr.substring(2, desStr.length()-2);
					}
				}
			}else{
				if(desStr.length() > 2){
					for(int i=0;i<desStr.length()-2;i++){
						re = re+"*";
					}
					v = desStr.substring(2, desStr.length());
				}else{
					re = "**";
					v = desStr;
				}
			}
			s = info.replaceAll(v,re);
			return s;
		}else{
			return "";
		}
	}


	/**
	 * 生成随机数
	 * 
	 * @param max
	 * @return
	 */
	public static int random(int max) {

		return new Random().nextInt(max);
	}

	/**
	 * 随机生成<code>num</code>位数字码
	 */
	public static String randomNum(int num) {

		Random r = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			sb.append(r.nextInt(10));
		}
		return sb.toString();
	}

	/**
	 * 随机6位数字码
	 */
	public static String randomNum6() {
		return randomNum(6);
	}

	/**
	 * 随机4位数字码
	 */
	public static String randomNum4() {
		return randomNum(4);
	}

	/**
	 * md5加密
	 * 
	 * @param value
	 *            要加密的值
	 * @return 失败，返回null
	 */
	public static String pwdMD5(String value) {

		if (StringUtils.isBlank(value)) {
			return null;
		}
		StringBuffer buf = new StringBuffer();
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			log.error("MD5加密错误 ", e);
			return null;
		}
		md.update(value.getBytes());
		byte b[] = md.digest();
		int i = 0;
		for (int offset = 0; offset < b.length; offset++) {
			i = b[offset];
			if (i < 0)
				i += 256;
			if (i < 16)
				buf.append("0");
			buf.append(Integer.toHexString(i));
		}
		return buf.toString().toUpperCase();
	}

	public static String encode(String param) {

		try {
			return URLEncoder.encode(param, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			
			log.error("encode error!!", e);
			return "";
		}
	}

	public static String decode(String param) {

		try {
			return URLDecoder.decode(param, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			
			log.error("decode error!!", e);
			return "";
		}
	}
	
	public static String htmlspecialchars(String param){
		
		if(param == null){
			return null;
		}
		return param.replaceAll("<" ,"&lt;").replaceAll(">" ,"&gt;");
	}
	
	
	public static String hex2binary(String hexString){  
		
        if (hexString == null || hexString.length() % 2 != 0)  
            return null;  
        String bString = "", tmp;  
        for (int i = 0; i < hexString.length(); i++)  
        {  
            tmp = "0000"  
                    + Integer.toBinaryString(Integer.parseInt(hexString  
                            .substring(i, i + 1), 16));  
            bString += tmp.substring(tmp.length() - 4);  
        }  
        return bString;  
    }  
	
	public static String binary2hex(String bString){  
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)  
            return null;  
        StringBuffer tmp = new StringBuffer();  
        int iTmp = 0;  
        for (int i = 0; i < bString.length(); i += 4)  
        {  
            iTmp = 0;  
            for (int j = 0; j < 4; j++)  
            {  
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);  
            }  
            tmp.append(Integer.toHexString(iTmp));  
        }  
        return tmp.toString();  
    } 
	/**
	 * isHave(判断字符串数组中是否含有某个字符串)
	 * @param strs
	 * @param s
	 * @return
	 * boolean
	 */
	public static boolean isHave(String strs[],String s){
		for(int i = 0; i < strs.length; i++){
			if(strs[i].indexOf(s) != -1){
				return true;
			}
		}
		return false;
	}
	
	
	public static List<String> toStrList(List<Integer> list){
		
		List<String> strs = new ArrayList<String>(list.size());
		for(Integer l : list)
		{
			strs.add(l.toString());
		}
		return strs;
	}
	
	public static String[] toStrArray(List<Integer> list){
	    
	    String[] strs = new String[list.size()];
	    for (int i = 0; i < list.size(); i++) {
            strs[i] = list.get(i).toString();
        }
	    
	    return strs;
	}
	
	
	public static List<Integer> toIntList(List<String> list){
	    
	    List<Integer> ints = new ArrayList<>();
	    for (String str : list) {
	        ints.add(str2Int(str));
	    }
	    
	    return ints;
	}
	
    public static Integer str2Int(String str) {
        Integer in = null;
        if (StringUtils.isNotBlank(str)) {
            try {
                in = Integer.parseInt(str);
            } catch (NumberFormatException e) {
                log.warn(str + "不能转换为整型！");
            }
        }
        return in;
    }

}